<?php
/*
 * 外签合同
*/
header("Content-Type: text/html;charset=UTF-8");
class Outside_contract extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'pagination' );
		$this->load->helper(array('form', 'url'));
	}

	public function index($offset = '') {
		$user_name=$this->session->userdata('username');
		if($user_name){
			$this->load->model ( 'outside_contract_model', 'contactm' );
			$limit = 10;
			$total = $this->contactm->count_nums ();
			$data['total']=$total;
			$data['offset']=$offset;
			$data ['contract'] = $this->contactm->list_contracters ( $limit, $offset );
			$current_total=count($data ['contract']);
			$data['current_total']=$offset+$current_total;
			
			$config ['base_url'] = base_url () . 'index.php/outside_contract/index/';
			$config ['total_rows'] = $total;
			$config ['per_page'] = $limit;
			$config ['num_links'] = 4;
			$config ['first_link'] = '首页';
			$config ['last_link'] = '尾页';
			$this->pagination->initialize ( $config );
			$data ['pag_links'] = $this->pagination->create_links ();
			
			$data ['page_title'] = '外签合同 — 昊美科技CRM系统';
			$data ['class9'] = 'cur';
			$data ['all_users'] = $this->get_all_users();
			$this->load->view ( 'outside_contract_list', $data );
		}else{
			$this->load->view ( 'login');
		}
	}

	/*
	 * 客户名查询记录
	*/
	public function check($name = ''){
		//$this->db->select('customer.customer_name as customer_name,outside_contract.*,opportunity.id as opportunity_id,user.user_name as user_name')->from('customer')->join('outside_contract', 'outside_contract.customer_id = customer.id')->join('opportunity', 'outside_contract.opportunity_id = opportunity.id')->join('user','outside_contract.owner_id=user.id')->like('outside_contract.subject',urldecode($name))->order_by('outside_contract.signing_time', "desc");
		$this->db->select('outside_contract.*,user.user_name as user_name,suppliers.name as suppliers_name')->from('outside_contract')->join('user','outside_contract.owner_id=user.id')->join('suppliers','outside_contract.suppliers_id=suppliers.suppliers_id')->where('outside_contract.subject',urldecode($name));
		$query = $this->db->get();
		$data['contract'] = $query->result();
		$data ['pag_links'] = '';
		$data ['page_title'] = '外签合同 — 昊美科技CRM系统';
		$data ['class9'] = 'cur';
		$data ['all_users'] = $this->get_all_users();
		$this->load->view ( 'outside_contract_list', $data );
	}

	/**
	 * 数据所有者查询
	 * */
	public function check_user_messages($id = ''){
		$data ['pag_links'] = '';
		$data ['page_title'] = '外签合同 — 昊美科技CRM系统';
		$data ['class9'] = 'cur';
		$query = $this->db->query('SELECT * FROM outside_contract join user  on outside_contract.owner_id = user.id where owner_id = '.$id.' order by outside_contract.create_time desc');
		$data['contract'] = $query->result();
		$data ['all_users'] = $this->get_all_users();
		$this->load->view ( 'outside_contract_list', $data );
	}
	
	/*
	 * 查看某条记录
	*/
	public function find($id = '') {
		//$this->db->select('customer.customer_name as customer_name,outside_contract.*,user.user_name as user_name,suppliers.name as suppliers_name')->from('customer')->join('outside_contract', 'outside_contract.customer_id = customer.id')->join('user','outside_contract.owner_id=user.id')->join('suppliers','outside_contract.suppliers_id=suppliers.suppliers_id')->where('outside_contract.contract_id',$id);
		$this->db->select('outside_contract.*,user.user_name as user_name,suppliers.name as suppliers_name')->from('outside_contract')->join('user','outside_contract.owner_id=user.id')->join('suppliers','outside_contract.suppliers_id=suppliers.suppliers_id')->where('outside_contract.contract_id',$id);
		$query = $this->db->get();
		$data['find_contract'] = $query->result();
		$this->load->view ( 'outside_contract_view',$data);
	}

	/*
	 * 删除记录
	*/
	public function delete($id = '') {
		$row = $this->db->delete ( 'outside_contract', array ('contract_id' => $id ) );
		if($row > 0){
			redirect ( 'outside_contract/index/' );
		}
	}

	/*
	 * 查看某条编辑记录
	*/
	public function edit($id = '') {
		//$this->db->select('customer.customer_name as customer_name,customer.id as customer_id,outside_contract.*,opportunity.id as opportunity_id,opportunity.subject as opportunity_subject,user.id as user_id,user.user_name as user_name,suppliers.name as suppliers_name')->from('customer')->join('outside_contract', 'outside_contract.customer_id = customer.id')->join('opportunity', 'outside_contract.opportunity_id = opportunity.id')->join('user','outside_contract.owner_id=user.id')->join('suppliers','outside_contract.suppliers_id=suppliers.suppliers_id')->where('outside_contract.contract_id',$id);
		$this->db->select('outside_contract.*,user.user_name as user_name,suppliers.name as suppliers_name')->from('outside_contract')->join('user','outside_contract.owner_id=user.id')->join('suppliers','outside_contract.suppliers_id=suppliers.suppliers_id')->where('outside_contract.contract_id',$id);
		$query = $this->db->get();
		$data['update_contract'] = $query->row();
		$this->load->view ( 'outside_contract_edit', $data );
	}

	/*
	 * 新增客户
	*/
	public function show_contract_add(){ /*显示增加页面*/
		$this->load->view ( 'outside_contract_add');
	}

	public function insert(){
		$data_array = $_POST;
		
		if($data_array['textfield'] != ''){
			/*上传文件*/
			$filerepository = getcwd()."/upload/";
			if(!is_dir($filerepository)){ /*判断给定文件名是否是一个目录*/
				mkdir ($filerepository);
			}
			
			$config['upload_path'] = $filerepository;
			$config['allowed_types'] = 'pdf|gif|jpg|png|txt|xlsx|docx|xls|doc';
			$config['max_size'] = '20480';
			$config['max_width']  = '20480';
			$config['max_height']  = '20480';
			$config['overwrite']  = false;
			$config['encrypt_name']  = true;
			
			$this->load->library('upload', $config);
			
			if ( (! $this->upload->do_upload())){
				//$error = array('error' => $this->upload->display_errors());
				$this->load->view ( 'update_error');
			}else{
				$data = array('upload_data' => $this->upload->data());
				$file_name = $data['upload_data']['file_name'];/*加密名*/
				$client_name = $data['upload_data']['client_name']; /*客户端文件名*/
				/*保存数据*/
				$data_array = $_POST;
				$data_array['text_attachments'] = $file_name;
				$data_array['real_file_name'] = $client_name;
			}
		}
		unset($data_array['textfield']);
		/*
		 * 获取供应商编号,拥有者编号,合同/订单编号
		*/
		$owner_id = '';
		
		$this->db->select ( 'id' )->from ( 'user' )->where ( 'user_name', trim($data_array ['owner_name']) );
		$query = $this->db->get ()->result ();
		$owner_id = $query[0]->id; /*拥有者id*/
		
		$this->db->select ( 'suppliers_id' )->from ( 'suppliers' )->where ( 'name', trim($data_array ['suppliers_name']) );
		$query = $this->db->get ()->result ();
		$suppliers_id = $query[0]->suppliers_id; /*供应商编号*/
		
		$this->db->select ( 'contract_id' )->from ( 'contract' )->where ( 'subject', trim($data_array ['order_name']));
		$query = $this->db->get ()->result ();
		$order_id = $query[0]->contract_id; /*合同/订单编号*/
		
		$data_array ['owner_id']=$owner_id;
		$data_array ['suppliers_id']=$suppliers_id;
		$data_array ['order_id']=$order_id;
		unset($data_array ['order_name']);
		unset($data_array ['owner_name']);
		unset($data_array ['suppliers_name']);
		
		date_default_timezone_set ( 'PRC' );
		$now_time = date ( 'Y-m-d H:i:s', strtotime ( 'now' ) );
		$data_array ['create_time'] = $now_time;

		$q = $this->db->insert('outside_contract', $data_array);
		if($q > 0){
			redirect ( 'outside_contract/index/' );
		}else{
		}
	}

	/*
	 * 编辑某条纪律
	*/
	public function update() {
		$data_array = $_POST;
		
		if($data_array['textfield'] != ''){
			/*上传文件*/
			$filerepository = getcwd()."/upload/";
			$agofilerepository = getcwd()."/ago_upload/";
			if(!is_dir($filerepository)){ /*判断给定文件名是否是一个目录*/
				mkdir ($filerepository);
			}
			if(!is_dir($agofilerepository)){ 
				mkdir ($agofilerepository);
			}
			$config['upload_path'] = $filerepository;
			$config['allowed_types'] = 'pdf|gif|jpg|png|txt|xlsx|docx|xls|doc';
			$config['max_size'] = '20480';
			$config['max_width']  = '20480';
			$config['max_height']  = '20480';
			$config['overwrite']  = false;
			$config['encrypt_name']  = true;
			
			$this->load->library('upload', $config);
			
			if ( ! $this->upload->do_upload()){ /*上传失败*/
				//$error = array('error' => $this->upload->display_errors());
				$data_array['text_attachments'] = $data_array['hitext'];
				$data_array['real_file_name'] = $data_array['hireal'];
				unset($data_array['hitext']);
				unset($data_array['hireal']);
				$this->load->view ( 'update_error');
			}else{
				$data = array('upload_data' => $this->upload->data());
				$contract_id = $data_array['contract_id'];
				
				$query = $this->db->query("SELECT text_attachments FROM outside_contract where contract_id = $contract_id");
				$row = $query->row_array();
				$text_attachments=$row['text_attachments'];/*获取加密名*/
				
				if($text_attachments){
					if(file_exists($filerepository.$text_attachments)){ /*检查文件或目录是否存在*/
						if(!rename($filerepository.$text_attachments,$agofilerepository.$text_attachments)){ /*移动文件失败*/
							//redirect ( 'contract/index/' );
							echo '移动文件失败';
						}
					}else{
						echo '源文件未找到';
					}
				}
				
				$file_name = $data['upload_data']['file_name'];/*加密名*/
				$client_name = $data['upload_data']['client_name']; /*客户端文件名*/
				
				$data_array['text_attachments'] = $file_name;
				$data_array['real_file_name'] = $client_name;
			}
		}
		
			unset($data_array['textfield']);
			$contract_id = $data_array['contract_id'];
			
			$user_id='';
			if($data_array['hiusname'] != $data_array['user_name']){ /*获取用户编号*/
				$this->db->select ( 'id' )->from ( 'user' )->where ( 'user_name', $data_array ['user_name'] );
				$query = $this->db->get ();
				foreach ( $query->result () as $rows ) {
					$user_id = $rows->id;
				}
				$data_array['owner_id']=$user_id;
			}
			
			$query = $this->db->query('SELECT suppliers_id FROM suppliers where name = "'.$data_array['suppliers_name'].'"');
			$row = $query->row_array();
			$data_array['suppliers_id'] = $row['suppliers_id'];
			
			unset($data_array['suppliers_name']);
			unset($data_array['contract_id']);
			unset($data_array['hiusname']);
			unset($data_array['user_name']);
			unset($data_array['hitext']);
			unset($data_array['hireal']);
			
			$this->db->where('contract_id', $contract_id);
			$row=$this->db->update('outside_contract', $data_array);
			if($row > 0){
				redirect ( 'outside_contract/index/' );
			}
	}

	/*查询对应机会*/
	public function check_opportunity(){
		$data_array = $_POST;
		/*获取对应客户编号*/
		$this->db->select ( 'id' )->from ( 'customer' )->where ( 'customer_name', $data_array ['name'] );
		$query = $this->db->get ();
		$row = $query->row();
		$customer_id = $row->id;
		/*获取客户对应的销售机会*/
		$this->db->select ( 'id,subject' )->from ( 'opportunity' )->where ( 'customer_id', $customer_id );
		$query = $this->db->get ();
		$opportunity_array = $query->result_array();
		$json_string = json_encode($opportunity_array);
		echo $json_string;
	}

	/*自动搜索获取合同*/
	public function get_contract(){
		$data_array = $_POST;
		/*获取对应客户编号*/
		$this->db->select ( 'id' )->from ( 'customer' )->where ( 'customer_name', $data_array ['name'] );
		$query = $this->db->get ();
		$row = $query->row();
		$customer_id = $row->id;

		$this->db->select ( 'contract_id,subject' )->from ( 'contract' )->where ( 'customer_id', $customer_id );
		$query = $this->db->get ();
		$opportunity_array = $query->result_array();
		$json_string = json_encode($opportunity_array);
		echo $json_string;
	}
	
	/*获取所有用户*/
	public function get_all_users(){
		$query = $this->db->query("SELECT * FROM user order by create_time asc");
		return $query->result();
	}
	
	public function list_user_contracters($user_id = 0){ /*获取分页contacter数据*/
		$contracts = array();
		if($user_id == 0){
			redirect ( 'outside_contract/index/' );
		}else{
			$query = $this->db->where(owner_id,$user_id)->from('outside_contract')->order_by("create_time", "desc")->limit($limit, $offset)->get();
			
			foreach($query->result() as $row) {
				$customer_query = $this->db->get_where ( 'customer',array ('id' => $row->customer_id), $condition_array );
				$customer = $customer_query->row();
				$row->customer_name = $customer->customer_name;
			
				$user_query = $this->db->get_where ( 'user', array ('id' => $row->owner_id ) );
				$user = $user_query->row();
				$row->user_name = $user->user_name;
			
				$contracts[] = $row;
			}
			return $contracts;
		}
	}
	
	public function autosearch($name = ''){  /*自动搜索供应商*/
		if($name){
			$contacter_names =array();
			$contacter_names['q'] = urldecode($name);
			$contacter_names['p'] = true;
			$contacter_names['s'] = array();

			$this->db->select('name')->like('name', urldecode($name))->from('suppliers');
			$query = $this->db->get();
			foreach($query->result() as $row){
				$contacter_names['s'][] = $row->name;
			}
		
			$json_string = json_encode($contacter_names);
			echo "window.baidu.sug($json_string)";
		}
	}
	
	/*合同编号唯一性检测*/
	public function check_outside_contract_number(){
		$data_array = $_POST;
		$query = $this->db->query("SELECT count(*) as nums FROM outside_contract where contract_no = '".$data_array['number']."'");
		$row = $query->row();
		$nums = $row->nums;
		if($nums){
			echo $nums;
		}else{
			echo '';
		}
	}
	
	/*下载附件*/
	public function download($id = ''){
		$query = $this->db->query("SELECT text_attachments,real_file_name FROM outside_contract where contract_id = $id");
		$row = $query->row();
		$file_name=$row->text_attachments;
		$real_file_name=$row->real_file_name;
		
		$this->load->helper('download');
		$filerepository = getcwd()."/upload/";
		if(!file_exists($filerepository.$file_name)){
			echo '源文件未找到';
		}else{
			$data = file_get_contents($filerepository.$file_name); // 读文件内容
			$name = $real_file_name;
			force_download($name, $data);
		}
	}
	
	
}
?>